﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web.Validation;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.User {
    public partial class Create : PageSupport {
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
                this.txt_UserNo.Text = biz.GenerateUserNo();
            }
        }

        void InitForm() {
            this.ddl_Role.DataValueField = "RoleKey";
            this.ddl_Role.DataTextField = "RoleName";
            this.ddl_Role.DataSource = biz.LoadAllRoles(this.Power.RoleLevel);
            this.ddl_Role.DataBind();

            this.txt_UserNo.Attributes.Add("readonly", "readonly");
            this.txt_UserNo.CssClass = "input_readonly";

        }
        void FillForm(String key) {

        }

        private void BindNormalData(ref TSysUserEO eo) {
             try {
                eo.Birthday = Convert.ToDateTime(this.txt_Birthday.Text);
            } catch {
                eo.Birthday = DateTime.Now;
            }
            eo.Gender = Convert.ToInt32(this.ddl_Gender.SelectedValue);
            eo.ActivatedFlg = Convert.ToInt32(this.ddl_Activate.SelectedValue);
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_UserName.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            if (!String.IsNullOrEmpty(this.txt_Password.Text) && this.txt_Password.Text != this.txt_Password2.Text) {
                SiteInfo.Send(Page, TxtDef.ERROR, "两次输入的密码不匹配，请重新输入");
                return;
            }
            if (BizConst.InvalidRegisterName.IndexOf(this.txt_LoginName.Text+",")!=-1) {
                //非法的用户名
                BizFactory.Get<ILogBiz>().Info("试图注册非法的用户名：" + this.txt_LoginName.Text, this.Actor, Request);
                SiteInfo.Send(Page, TxtDef.ERROR, "登录名(" + this.txt_LoginName.Text + ")已存在，请重新输入");
                return;
            }
            if (String.IsNullOrEmpty(this.txt_Password.Text)) {
                SiteInfo.Send(Page, TxtDef.ERROR, "请输入密码");
                return;
            }
            //loginName 和 email不能重复
            TSysUserEO eo = new TSysUserEO();
            eo.Password = this.txt_Password.Text;
            eo.Password = ClientBiz.Md5(eo.Password);
            eo.ActivatedFlg = 1;
            if (biz.IfEmailExists(this.txt_Email.Text, eo.UserKey)) {
                SiteInfo.Send(Page, TxtDef.ERROR, "Email(" + this.txt_Email.Text + ")已存在，请重新输入");
                return;
            }
            eo.Email = this.txt_Email.Text;
            if (biz.IfLoginNameExists(this.txt_LoginName.Text, eo.UserKey)) {
                SiteInfo.Send(Page, TxtDef.ERROR, "登录名(" + this.txt_LoginName.Text + ")已存在，请重新输入");
                return;
            }
            if (biz.IfUserNameExists(this.txt_UserName.Text, eo.UserKey)) {
                SiteInfo.Send(Page, TxtDef.ERROR, "用户名(" + this.txt_UserName.Text + ")已存在，请重新输入");
                return;
            }
            if (biz.IfUserNoExist(this.txt_UserNo.Text, eo.UserKey)) {
                SiteInfo.Send(Page, TxtDef.ERROR, "用户编号已经存在，请重新提交");
                this.txt_UserNo.Text = biz.GenerateUserNo();
                return;
            } else {
                eo.UserNo = this.txt_UserNo.Text;
            }
            eo.LoginName = this.txt_LoginName.Text;
            eo.CreateDate = DateTime.Now;
            eo.RoleKey = this.ddl_Role.SelectedValue;
            eo.UserName = this.txt_UserName.Text;
            eo.OwnerKey = this.OwnerKey;
            eo.UserNo = biz.GenerateUserNo();
            eo.DeptKey = this.hid_Dept.Value;
            BindNormalData(ref eo);
            biz.Save<TSysUserEO>(eo);
            BizFactory.Get<ILogBiz>().LogInsert("用户", eo.LoginName, this.Actor, Request);
            Response.Redirect("List.aspx");
        }
    }
}
